package service

import (
	"fmt"
	"goapp-web-server/app/models"
	"goapp-web-server/app/utils/orm"
)

type SysMenuService interface {
	GetAll() ([]models.SysMenu, error)
	GetByID(id string) (models.SysMenu, error)
}

func NewMenuService() SysMenuService {
	return new(menuService)
}

type menuService struct {
}

// menu list
func (s *menuService) GetAll() ([]models.SysMenu, error) {
	engine := orm.Engine
	list := make([]models.SysMenu, 0)
	err := engine.Table("sys_menu").
		Select(`sys_menu.*`).
		Where("sys_menu.deleted_at is NULL").
		Limit(10, 0).
		Find(&list)
	if err != nil {
		fmt.Println("error:", err)
		return nil, err
	}
	return list, nil
}

// get category by id
func (s *menuService) GetByID(id string) (models.SysMenu, error) {
	engine := orm.Engine
	menu := models.SysMenu{}
	_, err := engine.Table("sys_menu").
		Select(`sys_menu.*`).
		Where("sys_menu.id = ?", id).
		Get(&menu)

	fmt.Println(menu.Name)
	if err != nil {
		fmt.Println("error:", err)
		return menu, err
	}
	return menu, nil
}
